package com.lakudouzi.flink.cdc.config;//package com.lakudouzi.flink.cdc.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class ElasticsearchHandle {

    @Autowired
    private ElasticsearchClient client;

    /**
     * 判断索引是否存在
     */
    public Boolean hasIndex(String indexName) throws IOException {
        BooleanResponse exists = client.indices().exists(d -> d.index(indexName));
        return exists.value();
    }

    /**
     * 删除索引
     */
    public Boolean deleteIndex(String indexName) throws IOException {
        DeleteIndexResponse delete = client.indices()
                .delete(d -> d.index(indexName));
        return delete.acknowledged();
    }

    /**
     *  创建索引
     */
    public Boolean createIndex(String indexName) throws IOException {
        CreateIndexResponse indexResponse = client.indices().create(c -> c.index(indexName));
         return indexResponse.acknowledged();
    }








}
